package com.UI;

import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.apache.log4j.Logger;

import com.business.InfoBean;
import com.business.InfoCrawler;
import com.business.WriteInfoToXML;
import com.util.XmlUtil;

public class InfoUI extends JFrame
{

	/**
	 * @author bob
	 * @date 2018/3/26 TODO Design UI
	 */
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(InfoUI.class);

	private JFrame jf;

	public void CreateUI(String title, final String projectPath)
	{
		logger.info("Welcome use me!!!");
		jf = new JFrame(title);
		jf.setBounds(570, 270, 300, 200);
		jf.setResizable(false);
		jf.setLayout(null);
		Container cp = jf.getContentPane();
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e)
		{
			System.exit(0);
		}
		jf.setIconImage(Toolkit.getDefaultToolkit().createImage(projectPath + "/clock.ico"));
		final JLabel infoTip = new JLabel("愿你有个好心情O(^_^)O,欢迎使用！");
		infoTip.setFont(new Font("微软雅黑", 1, 14));
		final JButton btnUpdate = new JButton("更新本地");
		final JButton btncheck = new JButton("检查信息");
		infoTip.setBounds(35, 50, 240, 50);
		btnUpdate.setBounds(35, 130, 100, 30);
		btncheck.setBounds(155, 130, 100, 30);
		cp.add(infoTip);
		cp.add(btnUpdate);
		cp.add(btncheck);
		if(!XmlUtil.isXmlExist(projectPath))
		{
			btncheck.setEnabled(false);
		}
		jf.setVisible(true);
		btnUpdate.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO 手动更新信息列表，用于与后面爬取信息比对
				try
				{
					btnUpdate.setEnabled(false);
					btncheck.setEnabled(false);
					WriteInfoToXML wx = new WriteInfoToXML();
					ArrayList<InfoBean> infoList;
					infoList = InfoCrawler.getInfo();
					wx.writeInfoFirst(projectPath, infoList);
					JOptionPane.showMessageDialog(jf, "更新本地信息完成！", "提示", JOptionPane.INFORMATION_MESSAGE);
					logger.info("更新本地信息成功");
					btnUpdate.setEnabled(true);
					btncheck.setEnabled(true);
				}
				catch (Exception e1)
				{
					logger.error("更新失败，原因:"+e1.toString());
					JOptionPane.showMessageDialog(jf, "更新失败，原因:"+e1.toString(), "提示", JOptionPane.INFORMATION_MESSAGE);
					btnUpdate.setEnabled(true);
					btncheck.setEnabled(true);
				}

			}
		});
		btncheck.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					btnUpdate.setEnabled(false);
					btncheck.setEnabled(false);
					ArrayList<InfoBean> newList = InfoCrawler.getInfo();
					if (XmlUtil.isUpdate(newList, projectPath))
					{
						//在检查的同时更新替换本地xml文件
						WriteInfoToXML wx = new WriteInfoToXML();
						wx.writeInfoFirst(projectPath, newList);
						infoTip.setBounds(60, 50, 200, 30);
						infoTip.setText("有新内容，请登录官网查看");
						logger.info("有新内容，请登录官网查看");
						
					}
					else
					{
						infoTip.setBounds(95, 50, 130, 50);
						infoTip.setText("暂无新内容更新！");
						logger.info("暂无新内容更新！");
					}
					btnUpdate.setEnabled(true);
					btncheck.setEnabled(true);
				}
				catch (Exception e1)
				{
					logger.error("运行出现错误:"+e1.getMessage());
					infoTip.setBounds(70, 50, 150, 30);
					infoTip.setText("运行错误，请及时处理");
					btnUpdate.setEnabled(true);
					btncheck.setEnabled(true);
				}
			}
		});

	}

}
